<?php
/**
 * 商品分类控制器
 */
Class CategoryAction extends CommonAction {

	/**
	 * 分类列表视图
	 */
	Public function index () {
		$result = M('category')->select();
		// p($result);die;
		$cate = recursion($result, 0, 2, 3,0);
		foreach ($cate as $k => $v) {
			$where = array('id' => $v['tid']);
		    $type = M('type')->where($where)->find();
		    $cate[$k]['tid']=$type['name'];
		}
		// p($cate);die;
		$this->cate = $cate;
		$this->display();
	}

	/**
	 * 添加商品分类视图
	 */
	Public function addCate () {
		$i = isset($_GET['pid']) ? 1 : 0;
		if ($i) {
			$pid = (int) $_GET['pid'];
			$this->pid = $pid;
		}
		$result = M('category')->select();
		// p($result);die;
		$cate = recursion($result, 0, 2, 3,0);
		// p($cate);
		$type = M('type')->select();
		$this->cate = $cate;
		$this->type = $type;
		$this->display();
	}

	/**
	 * 添加分类操作
	 */
	Public function runAddCate () {
		// p($_POST);die;
		$data = array(
			'name' => $_POST['name'],
			'pid' => isset($_POST['pid']) ? (int) $_POST['pid'] : 0,
			'tid' => isset($_POST['tid']) ? (int) $_POST['tid'] : 0,
			);
		if (M('category')->data($data)->add()) {
			$this->success('添加成功', U('index'));
		} else {
			$this->error('添加失败');
		}
	}


	/**
	 * 编辑商品分类视图
	 */
	Public function editCate () {
		$i = isset($_GET['id']) ? 1 : 0;
		if ($i) {
			$id = (int) $_GET['id'];
			$where = array('id' => $id);
		    $edit = M('category')->where($where)->find();
			$this->id = $id;
			$this->edit = $edit;
		}
		$result = M('category')->select();
		// p($result);die;
		$cate = recursion($result, 0, 2, 3,0);
		// p($cate);
		$type = M('type')->select();
		$this->cate = $cate;
		$this->type = $type;
		$this->display();
	}


	/**
	 * 修改分类操作
	 */
	Public function runEditCate () {
		// p($_POST);die;
		$data = array(
			'name' => $_POST['name'],
			'pid' => isset($_POST['pid']) ? (int) $_POST['pid'] : 0,
			'tid' => isset($_POST['tid']) ? (int) $_POST['tid'] : 0,
			);
		$id = (int) $_POST['id'];
		$where = array('id' => $id);
		if (M('category')->where($where)->data($data)->save()) {
			$this->success('修改成功', U('index'));
		} else {
			$this->error('修改失败');
		}
	}








}
?>